package com.zmh.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * 页面响应返回结果
 */
public class Result extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;

	public Result() {
		put("code", 0);
		put("msg","success");
	}

	public static Result error() {
		return error(500, "未知异常，请联系管理员");
	}

	public static Result error(String msg) {
		return error(500, msg);
	}

	public static Result error(int code, String msg) {
		Result r = new Result();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static Result ok(Object msg) {
		Result r = new Result();
		r.put("msg", msg);
		return r;
	}


	public static Result ok(Map<String, Object> map) {
		Result r = new Result();
		r.putAll(map);
		return r;
	}
	
    public static Result ok(String key, Object value) {
        Result r = new Result();
        r.put(key, value);
        return r;
    }
    
	public static Result ok() {
		return new Result();
	}

	@Override
	public Result put(String key, Object value) {
		super.put(key, value);
		return this;
	}
	
	public Result data(Object value) {
		super.put("data", value);
		return this;
	}
	
	@SuppressWarnings("unchecked")
    public Result data(String key,Object value) {
		Map<String,Object> data = (Map<String, Object>) this.get("data");
        if(null == data) {
            data = new HashMap<String,Object>();
        }
        data.put(key, value);
        data(data);
        return this;
    }
}